<?php
namespace app\admin\controller\sum\store;

use addons\sum\service\store\Agent as StroeService;
use think\Db;
use think\exception\HttpResponseException;
use addons\sum\exception\SumException;
use app\admin\model\sum\store\Goods as StoreGoodsModel;
use app\admin\model\sum\goods\Goods as GoodsModel;

/**
 * 商户商品表
 */
class Goods extends \app\admin\controller\sum\Common
{

    public function _initialize()
    {
        \app\admin\controller\sum\Common::_initialize();
        $this->model = new StoreGoodsModel;
        $this->goodsModel = new GoodsModel();
    }

    /**
     * 商户商品列表
     */
    public function index()
    {
        $store_id = $this->request->param('store_id');
        if(!$store_id){
            $this->error('参数错误');
        }
        if (!$this->auth->isSuperAdmin()) {
            $admin=$this->auth->getUserInfo();
            if($admin['store_id']!=$store_id){
                $this->error('你不能操作该商户商品');
            }
        }
        if (!$this->request->isAjax()) {
            return $this->view->fetch();
        }

//        $storeGoodsIds = $this->model->where('store_id',$store_id)->group('goods_id')->column('goods_id');
//
//        $storeGoods = $this->goodsModel->sheepFilter()->with(['score_sku_prices'])
//            ->whereIn('id', $storeGoodsIds)
//            ->paginate($this->request->param('list_rows', 10))->each(function($goods) {
//                $goods->score_price = $goods->score_price;
//                $goods->score_sales = $goods->score_sales;
//                $goods->score_stock = $goods->score_stock;
//            });
//
//        $this->success('获取成功', null, $storeGoods);

        $records = $this->model->sheepFilter()->with(['goods'])->where('store_id',$store_id)->paginate($this->request->param('list_rows', 10));
        $this->success('获取成功', null, $records);
    }

    /**
     * 添加商户商品
     */
    public function add()
    {
        if (!$this->request->isAjax()) {
            return $this->view->fetch();
        }

        $params = $this->request->only([
            'goods_id', 'stock_price', 'price','store_id'
        ]);
        $this->svalidate($params, ".add");

        // 检查是否已经是积分商品了
        $count = $this->model->where('goods_id', $params['goods_id'])->where('store_id',$params['store_id'])->count();
        if ($count) {
            error_stop('该商品已经是该店铺商品了');
        }
        $this->model->save($params);
        $this->success('保存成功');
    }

    /**
     * 详情
     *
     * @param  $id
     * @return \think\Response
     */
    public function detail($id=null)
    {
        if($id){
            $detail = $this->model->with(['goods'])->where('id', $id)->find();
        }else{
            $store_id = $this->request->param('store_id');
            $goods_id = $this->request->param('goods_id');
            $detail = $this->model->with(['goods'])->where('store_id', $store_id)->where('goods_id', $goods_id)->find();
        }

        if (!$detail) {
            $this->error(__('No Results were found'));
        }
        $this->success('获取成功', null, $detail);
    }

    /**
     * 编辑商户商品
     */
    public function edit($id = null)
    {
        if (!$this->request->isAjax()) {
            return $this->view->fetch('add');
        }

        $params = $this->request->only([
            'stock_price',
            'price'
        ]);
        $this->svalidate($params, ".edit");

        Db::transaction(function () use ($id, $params) {
            $this->model->where('id', $id)->update($params);
        });
        $this->success('更新成功');
    }

}
